package org.hncors.gnssparse;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;


public class UISwitch extends View implements
		OnTouchListener,View.OnClickListener {

	private static final int TOUCH_MODE_IDLE = 0;
	private static final int TOUCH_MODE_DOWN = 1;
	private static final int TOUCH_MODE_DRAGGING = 2;

	private float mTouchX;
	private float mTouchY;

	private int mTouchMode;
	private int mTouchSlop;

	private boolean NowChoose = false;

	private boolean OnSlip = false;

	private float NowX;

	private Rect Btn_On, Btn_Off;

	private OnChangedListener mChgLsn;

	private Bitmap bg_on, bg_off, slip_btn;

	private int height;
	private int width;

	public UISwitch(Context context) {
		super(context);
		init();
	}

	public UISwitch(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public UISwitch(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	private void init() {
		
		bg_on = BitmapFactory.decodeResource(getResources(),
				R.drawable.switch_on);
		bg_off = BitmapFactory.decodeResource(getResources(),
				R.drawable.switch_off);
		slip_btn = BitmapFactory.decodeResource(getResources(),
				R.drawable.switch_thumb);
		
		Btn_On = new Rect(0, 0, slip_btn.getWidth(), slip_btn.getHeight());
		Btn_Off = new Rect(bg_off.getWidth() - slip_btn.getWidth(), 0,
				bg_off.getWidth(), slip_btn.getHeight());
		
		ViewConfiguration config = ViewConfiguration.get(getContext());
		mTouchSlop = config.getScaledTouchSlop();
		
		setOnTouchListener(this);
		setOnClickListener(this);
		
		setLayoutSize();
	}
	
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			switch (mTouchMode) {
			case TOUCH_MODE_IDLE:
				break;
			case TOUCH_MODE_DOWN:
				final float x = event.getX();
				final float y = event.getY();
				if (Math.abs(x - mTouchX) > mTouchSlop
						|| Math.abs(y - mTouchY) > mTouchSlop) {
					mTouchMode = TOUCH_MODE_DRAGGING;
					getParent().requestDisallowInterceptTouchEvent(true);
					return true;
				}
				break;
			case TOUCH_MODE_DRAGGING:
				OnSlip = true;
				NowX = event.getX();
				invalidate();
				return true;
			default:
				break;
			}
			break;
		case MotionEvent.ACTION_DOWN:
			final float x = event.getX();
			final float y = event.getY();
			if (isEnabled() && hitView(x, y)) {
				mTouchMode = TOUCH_MODE_DOWN;
				mTouchX = x;
				mTouchY = y;
			}
			break;

		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			if (mTouchMode == TOUCH_MODE_DRAGGING) {
				boolean LastChoose = NowChoose;
				if (event.getX() >= (bg_on.getWidth() / 2)) {
					NowX = bg_on.getWidth() - slip_btn.getWidth() / 2;
					NowChoose = true;
				}

				else {
					NowX = NowX - slip_btn.getWidth() / 2;
					NowChoose = false;
				}
				invalidate();
				if (mChgLsn != null && (LastChoose != NowChoose))
					mChgLsn.onChanged(NowChoose);
				return true;
			}
			mTouchMode = TOUCH_MODE_IDLE;
			OnSlip = false;
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Matrix matrix = new Matrix();
		Paint paint = new Paint();
		float x;
		{
			if (OnSlip) {
				if (NowX < (bg_on.getWidth() / 2))
					canvas.drawBitmap(bg_off, matrix, paint);
				else
					canvas.drawBitmap(bg_on, matrix, paint);
			} else {
				if (NowChoose)
					canvas.drawBitmap(bg_on, matrix, paint);
				else
					canvas.drawBitmap(bg_off, matrix, paint);
			}
			if (OnSlip){
			
				if (NowX >= bg_on.getWidth())
					x = bg_on.getWidth() - slip_btn.getWidth() / 2;
				else
					x = NowX - slip_btn.getWidth() / 2;
			} else {
				if (NowChoose)
					x = Btn_Off.left;
				else
					x = Btn_On.left;
			}
			if (x < 0)
				x = 0;
			else if (x > bg_on.getWidth() - slip_btn.getWidth())
				x = bg_on.getWidth() - slip_btn.getWidth();
			canvas.drawBitmap(slip_btn, x, 0, paint);
		}
	}
	
	public void setOnChangedListener(OnChangedListener l) {
		mChgLsn = l;
	}

	public interface OnChangedListener {
		 abstract void onChanged(boolean CheckState);
	}

	public void setChecked(boolean isChecked) {
		NowChoose = isChecked;
		invalidate();
		if(mChgLsn!=null)
			mChgLsn.onChanged(NowChoose);
	}
	
	public void setChecked(boolean isChecked, boolean call) {
		NowChoose = isChecked;
		invalidate();
		if(mChgLsn!=null && call == true)
			mChgLsn.onChanged(NowChoose);
	}
	
	public void setSelected(boolean isSelected) {
		NowChoose = isSelected;
		invalidate();
	}
	
	public boolean isChecked(){
		return NowChoose;
	}

	@Override
	public void onClick(View v) {
		setChecked(!NowChoose);
	}
	
	private boolean hitView(float x,float y){
		if(x>bg_on.getWidth() || y>bg_on.getHeight()){
			return false;
		}
		return true;
	}
	
	private void setLayoutSize(){
		Bitmap bm = BitmapFactory.decodeResource(getResources(),
		R.drawable.switch_off);
		height = bm.getHeight();
		width = bm.getWidth();
		if (isInEditMode()) { return; }
		bm.recycle();
	}
}
